package com.example.e_comm.exception;

import com.example.e_comm.dto.BaseResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BindException.class)
    public BaseResp<Void> handleValid(BindException e) {
        String msg = e.getBindingResult().getFieldError().getDefaultMessage();
        return BaseResp.fail(msg);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public BaseResp<Void> handleIA(IllegalArgumentException e) {
        return BaseResp.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResp<Void> handleAll(Exception e) {
        log.error("system error", e);
        return BaseResp.fail("系统繁忙");
    }
}